<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2019/12/2
 * Time: 23:11
 */
namespace app\api\controller;

use app\admin\model\Login as LoginModel;

class Login extends Controller
{
    public function test()
    {
        return $this->renderSuccess([], '请求成功');
    }

    public function index()
    {
        $data = $this->request->get();
        return $this->json(1001, '登录成功', $data);
    }

    public function login($username, $password)
    {
        $user = LoginModel::get(['username' => $username]);
        if (empty($user)) {
            return $this->renderError('用户不存在');
        }
        if ($user['password'] == $password) {
            $data = $this->request->param();
            //var_dump($data);
            $result = $this->validate($data, [
                'username' => 'require|min:2',
                'password' => 'require',
            ]);
            if (true !== $result) {
                //验证失败 输出错误信息
                // $this->success('注册成功','login/index');
                return $this->renderError('检查用户名或者密码的正确性');
            } else {
                session('username', $username);
                $_SESSION['n'] = $username;
                return $this->renderSuccess([], '登录成功');
            }
        } else {
            return $this->renderError('密码不正确');
        }
    }

    public function register()
    {
        //$data = $this->request->param();
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        $phone = $this->request->param('phone');
        $model = new LoginModel();
        $result = $model->save([
                'username' => $username,
                'password' => $password,
                'phone' => $phone
            ]);
            if ($result) {
                return $this->renderSuccess([], '注册成功');
            } else {
                return $this->renderError('注册失败');
            }
        }
}
